package com.gzhryc.shared_device.oem.micro.api.controllers.xcx.v1.member;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.MatrixToImageWriter;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberVerificationVoucher;
import com.gzhryc.shared_device.oem.code.member.services.MemberVerificationVoucherService;
import com.gzhryc.shared_device.oem.micro.api.MicroConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberVerificationVoucherListRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberVerificationVoucherListResponse;

public class MemberVerificationVoucherController {

    static Logger log = Logger.getLogger(MemberCouponController.class);

    @Route(value = { "/xcx/v1/member/verification_voucher/list.data" }, method = "POST")
    public MemberVerificationVoucherListResponse list(HttpServletRequest request) {
        MemberVerificationVoucherListResponse apiResp = new MemberVerificationVoucherListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        MemberVerificationVoucherListRequest apiReq = WebUtils.getJsonRequest(request, MemberVerificationVoucherListRequest.class);
        LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
        if (apiReq != null && member != null && apiReq.getBody().getType() != null) {
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/member/verification_voucher/qrcode.jpg" })
    public void qrcodeImg(HttpServletRequest request, HttpServletResponse response) {
        String id = WebUtils.getString(request, "id");
        if (StringUtils.isNotBlank(id)) {
            MemberVerificationVoucher verificationVoucher = MemberVerificationVoucherService.self().getById(id);
            if (verificationVoucher != null) {
                try {
                    String httpDomain = PropertyTools.get().getString("httpDomain");
                    String url = httpDomain + "/member/verification_voucher/" + verificationVoucher.getId();
                    WebUtils.exportJPEG(response);
                    MatrixToImageWriter.writeToStream(url, 400, 300, "jpeg", response.getOutputStream());
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
}
